package org.elzat.healthboxgatewayapp.util;

import lombok.experimental.UtilityClass;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;

import java.nio.charset.StandardCharsets;

@UtilityClass
public class ServerResponseUtil {

    public void setJsonHeaders(ServerHttpResponse response) {
        HttpHeaders headers = response.getHeaders();
        headers.set(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
        headers.set(HttpHeaders.CONTENT_ENCODING, "utf-8");
    }

    public DataBuffer writeJson2Buffer(ServerHttpResponse response, String json) {
        DataBuffer dataBuffer = response.bufferFactory().allocateBuffer();
        return dataBuffer.write(json.getBytes(StandardCharsets.UTF_8));
    }

}
